package com.team.abs.controller;

import java.sql.Timestamp;
import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.abs.entity.item_status;
import com.team.abs.service.*;

import net.sf.json.JSONObject;
@Controller
public class item_status_controller {
	@Autowired
	private item_status_service item_status_service;
	@Autowired
	private black_white_service black_white_service;
	
	@GetMapping("/item_status")
	public String item_status(){
		return "item_status";
	}
	//显示用户页面
	@RequestMapping(value="/user_status")	
	public String search_all(Model model,@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5")Integer pageSize,
			@RequestParam(value="user_uid",defaultValue = "")String user_uid ) {
		PageHelper.startPage(pageNum, pageSize);
		if(!user_uid.equals("")) {
			PageInfo<item_status> pageInfo =new PageInfo<item_status>(item_status_service.search_by_uid(user_uid));
			model.addAttribute("pageNum", pageInfo.getPageNum());
			model.addAttribute("pageSize", pageInfo.getPageSize());
			model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
			model.addAttribute("isLastPage",pageInfo.isIsLastPage());
			model.addAttribute("totalPages",pageInfo.getPages());
			model.addAttribute("pageInfo",pageInfo.getList());
			model.addAttribute("total",pageInfo.getTotal());
			model.addAttribute("jumpUrl","/user_status?pageNum=");
		}
		else {
			
			PageInfo<item_status> pageInfo =new PageInfo<item_status>(item_status_service.search_all());
			model.addAttribute("pageNum", pageInfo.getPageNum());
			model.addAttribute("pageSize", pageInfo.getPageSize());
			model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
			model.addAttribute("isLastPage",pageInfo.isIsLastPage());
			model.addAttribute("totalPages",pageInfo.getPages());
			model.addAttribute("pageInfo",pageInfo.getList());
			model.addAttribute("total",pageInfo.getTotal());
			model.addAttribute("jumpUrl","/user_status?pageNum=");
		}
		//这里出现null问题 卸载包后重装问题解决
		
		return "simple_user_status";
	}
	//显示物品页面
	//设置value值确保没有和entity层重名 需要和@GetMapping 对应的VALUE不重名 不然会冲突
	@RequestMapping(value="/item")	
	public String search_item_status(Model model,@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "8")Integer pageSize,
			@RequestParam(value="user_uid",defaultValue = "")String user_uid ,
			@RequestParam(value="item_name",defaultValue = "")String item_name ,
			@RequestParam(value="item_kind",defaultValue = "")String item_kind 
			) {
		PageHelper.startPage(pageNum, pageSize);
			PageInfo<item_status> pageInfo =new PageInfo<item_status>(item_status_service.search_by_user_uid_kind_status(user_uid, item_kind, item_name));
			model.addAttribute("pageNum", pageInfo.getPageNum());
			model.addAttribute("pageSize", pageInfo.getPageSize());
			model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
			model.addAttribute("isLastPage",pageInfo.isIsLastPage());
			model.addAttribute("totalPages",pageInfo.getPages());
			model.addAttribute("pageInfo",pageInfo.getList());
			model.addAttribute("total",pageInfo.getTotal());
			model.addAttribute("jumpUrl","/item?pageNum=");
		return "item_status";
	}
	//表单提交
	@ResponseBody
	@RequestMapping(value="/release_form_api",method=RequestMethod.POST)
	public String release_form(
			@RequestParam(value="user_uid",required=true)String user_uid,
			@RequestParam(value="item_kind",required=true)String item_kind,
			@RequestParam(value="item_name",required=true)String item_name,
			@RequestParam(value="item_limit_time",required=true)String item_limit_time,
			@RequestParam(value="item_price",required=true)int item_price
			){
		JSONObject result=new JSONObject();
		Timestamp time=Timestamp.valueOf(LocalDateTime.now().plusDays(Long.valueOf(item_limit_time)));
		item_status item_pip=new item_status(item_kind,item_name,user_uid,time,0,item_price);
		//System.out.println(item_pip.toString());
		String st="";
		if(black_white_service.search_black_man(user_uid, 1)>0) {
			
			st+="用户没有提交权限\n";
			result.put("status", st);return result.toString();
		}
		if(black_white_service.search_white_kind(item_kind)==0) {
			st+="物品不在白名单中\n";
			result.put("status", st);return result.toString();
		}
			
		if(item_status_service.insert_item(item_pip)<=0){
			st+="数据库异常\n";
			result.put("status", st);return result.toString();
		}
		result.put("status", "success");
		return result.toString();
	} 
}
